package controllers;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import models.Location;
import models.RatingNom;
import models.SolutionSubmission;

/*
public class ajxBestOfSitesConnectServlet {

	public List<HashMap<String, Object>> getAllBestOfSites(String _Region,
			String _Month) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String, Object>>();
		try {
			Objectify ofy = ObjectifyService.begin();
			boolean FlgFilterByRegion = false;
			boolean FlgFilterByMonth = false;
			String[] arrOfYear = _Month.split(" ");

			if (!(_Region.equals("--Select Region--") || _Region.equals(""))) {
				FlgFilterByRegion = true;
			}

*/

public class ajxBestOfSitesConnectServlet {
	
	public List<HashMap<String,Object>> getAllBestOfSites(String _Region, String _Month) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
    	try {
    		Objectify ofy = ObjectifyService.begin();
    	    boolean FlgFilterByRegion =false ;
    	    boolean FlgFilterByMonth =false ;
    	    String[] arrOfYear= _Month.split(" ");
    	    
    	    if (!(_Region.equals("--Select Region--") || _Region.equals("")) ){
    	    	FlgFilterByRegion =true;
    	    }
    	    
    	    if (!(_Month.equals("--Select Month--") || _Month.equals(""))){
    	    	FlgFilterByMonth =true;
    	    	arrOfYear  = _Month.split(" ");
    	    }
    	    
    	    Query<RatingNom> q ;
    	    if (FlgFilterByMonth) {
    	         q = ofy.query(RatingNom.class).filter("nominateSolutionMonth", arrOfYear[0].toString()).filter("nominateSolutionYear", arrOfYear[1].toString()).filter("isActive", true);
    	    }
    	    else{
    	    	 q = ofy.query(RatingNom.class).filter("isActive", true);
    	    }
    		
    		
    		
    		for ( RatingNom f : q.fetch() ) {
    			Objectify ofySolution = ObjectifyService.begin();
    			Query<SolutionSubmission> qSu = ofySolution.query(SolutionSubmission.class).filter("solID", f.getSolID());
    			if (FlgFilterByRegion) qSu.filter("LocationRegionName", _Region); //NEWLY ADDED BY SRVAN. REGION IS AVAILABE IN THE SOLUTION SUBMISSION
    					for ( SolutionSubmission fSu : qSu.fetch() ) {

			     			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    						itemMap.put("SolID",  f.getSolID());
    						itemMap.put("SolCode",  fSu.getSolCode()); //ADDED BY KULPRAPAS to get Solution Code
    						itemMap.put("Site",  fSu.getLocation()); //Changed from fSu.getProcessArea to fSu.getLocation by Kulprapas , OCT 17 2012
    						itemMap.put("Title", fSu.getTitle());
    						itemMap.put("customer", fSu.getCustomer());
    						itemMap.put("Month",  f.getNominateSolutionMonth() );
    						objList.add(itemMap);
    						
    						/*
    						boolean FlgHaveRegion =true;
    						if (FlgFilterByRegion){
    							FlgHaveRegion =false;
    							Objectify ofyLocation = ObjectifyService.begin();
        						Query<Location> qLocation = ofyLocation.query(Location.class).filter("LocationName",fSu.getProcessArea());
        						for ( Location fLocayion : qLocation.fetch() ) {
        							if (_Region.equals(fLocayion.getLocationRegionName())){
        								FlgHaveRegion =true;
        								break;
        							}
        						}
    						}
    						
    						
    						if (FlgHaveRegion){
			     			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    						itemMap.put("SolID",  f.getSolID());
    						itemMap.put("SolCode",  fSu.getSolCode()); //ADDED BY KULPRAPAS to get Solution Code
    						itemMap.put("Site",  fSu.getProcessArea());
    						itemMap.put("Title", fSu.getTitle());
    						itemMap.put("customer", fSu.getCustomer());
    						itemMap.put("Month",  f.getNominateSolutionMonth() );
    						objList.add(itemMap);
    						}
    						*/	
    			     }
    		}
    		
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	return objList;	
    	
    	/* THIS IS APPLE CODE COMMENTED
			if (!(_Month.equals("--Select Month--") || _Month.equals(""))) {
				FlgFilterByMonth = true;
				arrOfYear = _Month.split(" ");
			}

			Query<RatingNom> q;
			if (FlgFilterByMonth) {
				q = ofy.query(RatingNom.class)
						.filter("nominateSolutionMonth",
								arrOfYear[0].toString())
						.filter("nominateSolutionYear", arrOfYear[1].toString());
			} else {
				q = ofy.query(RatingNom.class);
			}

			for (RatingNom f : q.fetch()) {
				Objectify ofySolution = ObjectifyService.begin();
				Query<SolutionSubmission> qSu = ofySolution.query(
						SolutionSubmission.class).filter("solID", f.getSolID());
				for (SolutionSubmission fSu : qSu.fetch()) {
					if (FlgFilterByRegion) {
						if (fSu.getLocationRegionName().equalsIgnoreCase(
								_Region)) {
							HashMap<String, Object> itemMap = new HashMap<String, Object>();
							itemMap.put("SolID", f.getSolID());
							itemMap.put("SolCode", fSu.getSolCode()); 
							itemMap.put("Site", fSu.getProcessArea());
							itemMap.put("Title", fSu.getTitle());
							itemMap.put("customer", fSu.getCustomer());
							itemMap.put("Month", f.getNominateSolutionMonth());
							objList.add(itemMap);
						}
					} else {
						HashMap<String, Object> itemMap = new HashMap<String, Object>();
						itemMap.put("SolID", f.getSolID());
						itemMap.put("SolCode", fSu.getSolCode()); 
						itemMap.put("Site", fSu.getProcessArea());
						itemMap.put("Title", fSu.getTitle());
						itemMap.put("customer", fSu.getCustomer());
						itemMap.put("Month", f.getNominateSolutionMonth());
						objList.add(itemMap);
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return objList;
		*/
	}

}
